<?php
if (!defined('ZZQSS')){
	die("Access Denied");
}
/**
 *SupplierClass 商家操作類
 * add()
 * del()
 * getdata()
 *
 */
class SupplierClass {
	var $db;
	var $table;
	var $tablepre;
	function __construct(){
		//設置全局變量
		global $db,$tablepre;
		$this->db=$db;
		$this->tablepre=$tablepre;
		$this->table=$tablepre."suppliers";
	}
	function SupplierClass() {
		$this->__construct();
	}
	/**
	 * 添加商家數據入庫
	 *
	 * @param array $sarray
	 * @return OK/提示
	 */
	function add($sarray){
		if(is_array($sarray)){
			if(!empty($sarray['name']) && !empty($sarray['tel']) && !empty($sarray['address'])){
				//插入數據庫操作
				$wherestr="name='".$sarray['name']."' and "."tel='".$sarray['tel']."'";
				$temparray=$this->getdata(1,$wherestr);
				if(!empty($temparray))return $temparray[0]['sid'];
				else{
					$sid=inserttable($this->table, $sarray,1);
					if(is_numeric($sid))return $sid;
				}
			}return "缺少參數";
		}return "數據格式錯誤";
		
	}
	/**
	 * 編輯商家信息
	 *
	 * @param int $sid
	 * @param string $sarray
	 * @return OK/提示
	 */
	function edit($sid,$sarray){
		if(is_array($sarray) && !empty($sid)){
				//更新數據庫操作
				$wheresqlarr=" sid='{$sid}'";
				updatetable($this->table,$sarray, $wheresqlarr);
				return "OK";
		}return "參數錯誤";
	}
	/**
	 * 刪除商家信息
	 *
	 * @param int $sid
	 * @return OK/提示
	 */
	function del($sid){
		if(is_numeric($sid))
		{
			if(!$this->db->result_first("select count(*) from {$this->tablepre}groups where sid = '$sid'")){
			$this->db->query("delete from {$this->table} where sid={$sid}");
			return "OK";
			}else return "有相關團購，不能刪除";
		}else {
			return "參數錯誤";			
		}
		
		
	}
	
	function getselect($var="",$value="",$other=""){
		$arraydata=$this->getdata("","","","sid,name");
		if(!empty($arraydata)){
			foreach ($arraydata as $val){
				$arrayoption[$val['sid']]=$val['name'];
			}
			return getselectstr($var, $arrayoption, $value, $other);
		}
	}
	
	/**
	 * 獲取數據數組
	 *
	 * @param string $limit
	 * @param string $where
	 * @param string $orderby
	 * @return array
	 */
	function getdata($limit="",$where="",$orderby="",$field="*"){
		$tempdata=array();
		if(!empty($limit))$limit=" limit $limit ";
		if(!empty($where))$where=" where $where ";
		if(!empty($orderby))$orderby=" order by $orderby ";else $orderby=" order by sid desc";
		$sql="select {$field} from {$this->table}{$where}{$orderby}{$limit}";
		$query =$this->db->query($sql);
		while($value = $this->db->fetch_array($query)) {
			$tempdata[]=$value;
		}
		return $tempdata;
	
	}
	//獲取信息條數
	function getcount($where=""){
		if(!empty($where))$where=" where $where";
		$count= $this->db->result_first("Select count(*) From `$this->table` $where");
		return $count;
	
	}
}
?>